<extend name="template/base_index" />

<block name="area_header">

    <link rel="stylesheet" href="__CSS__/add_good.css">
    <link rel="stylesheet" type="text/css" href="__CSS__/mobile-select-area.css">
    <script type="text/javascript" src="__JS__/dialog.js"></script>
    <script type="text/javascript" src="__JS__/mobile-select-area.js"></script>
</block>

<block name="area_body">
    <div class="common_head">
        <a href="{:U('UserCenter/issue_his')}"><i class="fa fa-chevron-left"></i></a>
    </div>
    <form id="good_info" action="{:U('UserCenter/edit_product_post')}" method="post" enctype="multipart/form-data">
        <!--影藏的商品id-->
        <input type="hidden" name="product_id" value="{$productinfo['id']}">
        <div class="weui_cells_title"><input class="weui_input" type="text" placeholder="商品名称" name="name" value="{$productinfo['name']}"></div>
        <div class="weui_cells">
            <div class="weui_cell">
                <div class="weui_cell_bd weui_cell_primary">
                    <textarea class="weui_textarea" placeholder="用几句话描述你的宝贝......" rows="3" name="synopsis">{$productinfo['synopsis']}
                    </textarea>
                </div>
            </div>
            <div class="fb-imgcontent">
                <ul id="img_ul">
                    <volist name="imglist" id="img">
                        <li><i class="fa fa-close float_close"></i><img src="{$img['imgurl']}" alt="{$img['id']}" width="60px" height="60px"/></li>
                    </volist>
                    <li><img src="__IMG__/addimg.png" onclick="img_input.click();" width="60px" height="60px">
                        <input type="file" id="img_input"  style="display:none" name="wxshop"></li>
                </ul>
            </div>
        </div>
        <div class="weui_cells weui_cells_access">
            <a class="weui_cell" href="javascript:;">
                <div class="weui_cell_hd"><label class="weui_label">价格：</label></div>
                <div class="weui_cell_bd weui_cell_primary">
                    <input class="weui_input" type="text" placeholder="请输入价格" name="price" value="{$price['price']}">
                </div>
            </a>
            <a class="weui_cell" href="javascript:;">
                <div class="weui_cell_hd"><label class="weui_label">原价：</label></div>
                <div class="weui_cell_bd weui_cell_primary">
                    <input class="weui_input" type="text" placeholder="请输入价格" name="ori_price" value="{$price['ori_price']}">
                </div>
            </a>
            <a class="weui_cell" href="javascript:;">
                <div class="weui_cell_hd"><label class="weui_label">库存：</label></div>
                <div class="weui_cell_bd weui_cell_primary">
                    <input class="weui_input" type="number" placeholder="请输入库存" name="quantity" value="{$price['quantity']}">
                </div>
            </a>
            <a class="weui_cell" href="javascript:;">
                <div class="weui_cell_hd"><label class="weui_label">邮费：</label></div>
                <div class="weui_cell_bd weui_cell_primary">
                    <input class="weui_input" type="tel" placeholder="请输入邮费" name="postage" value="{$productinfo['postage']}">
                </div>
            </a>
            <a class="weui_cell" href="javascript:;">
                <div class="weui_cell_hd"><label class="weui_label">分类：</label></div>
                <div class="weui_cell_bd weui_cell_primary">
                    <input class="weui_input" id="txt_area2" type="text" value="{$catenamestr}" name="cate_list">
                    <input  type="hidden" value="{$cateidstr}" name="cate_id">
                </div>
            </a>
            <a class="weui_cell" href="javascript:;">
                <div class="weui_cell_hd"><label class="weui_label">货源：</label></div>
                <div class="weui_cell_bd weui_cell_primary">
                    <input class="weui_input" type="text" placeholder="请输入货源" name="dt_origin_country" value="{$productinfo['dt_origin_country']}">
                </div>
            </a>
        </div>
        <div class="fa-button">
            <a  class="weui_btn weui_btn_warn" onclick="submit_form()">确定发布</a>
        </div>
    </form>

    <script type="text/javascript">
        //级联分类
        var json ={$feilei};
        var selectArea2 = new MobileSelectArea();
        selectArea2.init({trigger:'#txt_area2',value:$('#txt_area2').data('value'),data:json,eventName:'click'});

        function close_event(){
            var imgIds = $(this).next().attr('alt');//这里仅仅是图片id没有商品的id
            var product_id = $("[name='product_id']").val();
            var arr = $(this).parent();
            $.get("{:U('Home/File/del_nowupload')}",{imgIds:imgIds,product_id:product_id},function (data){
                if(data){
                    arr.remove();
                }
            });
        }
        $(function() {
            $('#img_input').change(function() {
                //图片上传限制在6张
                var count = $("[name = 'img_list[]']").length;
                if((count + 1) >= 7){
                    alerttip("上传图片过多 最多6张");
                }else {
                    var file = this.files[0];
                    $.ajax({
                        url: "{:U('Home/File/uploadUserPicture')}",
                        type: 'POST',
                        cache: false,
                        data: new FormData($('#good_info')[0]),
                        processData: false,
                        contentType: false
                    }).done(function (res) {
                        console.log(res);
                        $('#img_ul').prepend('<li><i class="fa fa-close float_close"></i><img src="' + res['imgurl'] + '" alt="' + res['id'] + '" width="60px" height="60px"/><input type="hidden" value="' + res['id'] + '" name="img_list[]"></li>');
                        $(".float_close").bind("click",close_event);
                    }).fail(function (res) {
                        $.toptip('图片上传失败', 'error');
                    });
                }

            });
            //图片删除
            $(".float_close").bind("click",close_event);
        });

       //表单验证不完善
        function submit_form(){
            if($("[name='name']").val() == '' || $("[name='synopsis']").val() == '' || $("[name='img_list[]']").val() == '' || $("[name='price']").val() == '' || $("[name='ori_price']").val() == '' || $("[name='postage']").val() == '' || $("[name='dt_origin_country']").val() == ''){
                alerttip("有数据没填或有误");
            }else{
                $("[name='cate_id']").val($("[name='cate_list']").attr('data-value')) ;
                $('#good_info').submit();
            }
        }

        function alerttip(tipcontent){
            $('body').append('<div class="weui_toptips bg-error weui_toptips_visible" style="display: block;">'+ tipcontent +'</div>');
            window.setTimeout("$('.weui_toptips').remove();",2000);
        }
    </script>

</block>